#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using TVC.Layout;

#endregion

namespace TVC.Modules.CustomControl
{
    public partial class PopupBox : UserControl
    {
        public GridLayout Layout { get; set; }
        public MainForm MainForm { get; set; }
        string _lookup;
        private bool _nonValidated = false;

        [PropertyTab]
        public bool NonValidated
        {
            get { return _nonValidated; }
            set { _nonValidated = value; }
        }
        [PropertyTab]
        public Label LabelField { get; set; }
        public string Lookup
        {
            get { return _lookup; }
            set
            {
                if (value != null && _lookup != value)
                {
                    _lookup = value;
                    Layout = new GridLayout(MainForm.ServerPath + "Resources\\Layout\\", String.Format("{0}Documents\\Layout\\{1}\\", MainForm.ServerPath, MainForm.PODInfo.USER_ID), _lookup);
                }
            }
        }

        
        
        public TextBox TextBox
        {
            get { return textBox1; }
        }
        public bool Multiline
        {
            get { return textBox1.Multiline; }
            set { textBox1.Multiline = value; }
        }

        private void OpenView()
        {

            if (frm == null || frm.IsDisposed || !frm.Visible)
                Find();
            else if (!frm.Visible)
            {
                if (LabelField != null) frm.Text = LabelField.Text;
                frm.EnableBox = this.Enabled;
                frm.Visible = true;
            }
            //frm.ShowDialog();
        }
        public PopupBox()
        {
            InitializeComponent();
            textBox1.TextChanged += new EventHandler(textBox1_TextChanged);
            //cb.Leave += new EventHandler(cb_Leave);
            // cb.SelectionChangeCommitted += new EventHandler(cb_SelectionChangeCommitted);
            //cb.Validated += new EventHandler(cb_Validated);
            // cb.TextChanged += new EventHandler(cb_Validated);
        }

        void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (OnMyTextChanged != null) OnMyTextChanged(this, e);
        }





        public override string Text
        {
            get
            {
                return textBox1.Text;
            }
            set { textBox1.Text = value; }
        }

        public Button Button { get; set; }

        protected override void OnDoubleClick(EventArgs e)
        {
            OpenView();
            base.OnDoubleClick(e);
        }

        public event EventHandler OnMyTextChanged;

        public bool ReadOnly { get { return !textBox1.Enabled; } set { textBox1.Enabled = !value; } }
        Forms.frmPopupText frm;
        public void Find()
        {

            frm = new Forms.frmPopupText();
            frm.Value = textBox1.Text;
            frm.EnableBox = this.Enabled;
            if (LabelField != null) frm.Text = LabelField.Text;
            frm.FormClosed += (form, args) =>
            {
                if (((Forms.frmPopupText)form).DialogResult == DialogResult.OK)
                {
                    textBox1.Text = frm.Value;
                }
                textBox1.Focus();
            };
            frm.ShowDialog();

        }



        private void textBox1_DoubleClick(object sender, EventArgs e)
        {
            OpenView();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenView();
        }

        private void PopupBox_Load(object sender, EventArgs e)
        {
            if (Button != null) Button.Click += delegate { OpenView(); };
        }
    }
}